package com.link2room.quintesson.util;

import com.link2room.common.util.StringUtil;
import com.link2room.common.util.SysoptionUtil;
import com.link2room.enums.TaobaoMatchStatusEnum;
import com.link2room.enums.exception.QuintessonSysExceptionEnum;
import com.link2room.quintesson.exception.TaobaoException;
import com.link2room.quintesson.constant.TaobaoConstant;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sss on 2017/4/6.
 */
public class TaobaoUtil {
    public static Map<String,TaobaoClient> taobaoClientMap=new HashMap<>();

    public static void setTaobaoClient(String lhotelGroupCode) throws Exception {
        String taobaoURL = SysoptionUtil.getSysOption(lhotelGroupCode, TaobaoConstant.CONFIG_CATEGORY,TaobaoConstant.CONFIG_ITEM.URL);
        String taobaoAPPKEY = SysoptionUtil.getSysOption(lhotelGroupCode, TaobaoConstant.CONFIG_CATEGORY,TaobaoConstant.CONFIG_ITEM.APPKEY);
        String taobaoSECRET = SysoptionUtil.getSysOption(lhotelGroupCode, TaobaoConstant.CONFIG_CATEGORY,TaobaoConstant.CONFIG_ITEM.SECRET);
        if(taobaoClientMap.get(lhotelGroupCode)==null){
            TaobaoClient client=new DefaultTaobaoClient(taobaoURL, taobaoAPPKEY, taobaoSECRET);
            taobaoClientMap.put(lhotelGroupCode,client);
        }
    }

    public static TaobaoClient getTaobaoClient(String lhotelGroupCode) throws Exception {
        String taobaoURL = SysoptionUtil.getSysOption(lhotelGroupCode, TaobaoConstant.CONFIG_CATEGORY,TaobaoConstant.CONFIG_ITEM.URL);
        String taobaoAPPKEY = SysoptionUtil.getSysOption(lhotelGroupCode, TaobaoConstant.CONFIG_CATEGORY,TaobaoConstant.CONFIG_ITEM.APPKEY);
        String taobaoSECRET = SysoptionUtil.getSysOption(lhotelGroupCode, TaobaoConstant.CONFIG_CATEGORY,TaobaoConstant.CONFIG_ITEM.SECRET);
        TaobaoClient client=taobaoClientMap.get(lhotelGroupCode);
        if(client==null){
            client=new DefaultTaobaoClient(taobaoURL, taobaoAPPKEY, taobaoSECRET);
            if(client==null){
                throw new TaobaoException(QuintessonSysExceptionEnum.NO_TAOBAOCLIENT,"lhotelGroupCode:"+lhotelGroupCode+
                        "|taobaoURL:"+taobaoURL+"|taobaoAPPKEY:"+taobaoAPPKEY+"|taobaoSECRET:"+taobaoSECRET);
            }
            taobaoClientMap.put(lhotelGroupCode,client);
        }
        return client;
    }

    public static String getTaobaoSessionKey(String lhotelGroupCode)throws Exception{
        String taobaoSessionKey = SysoptionUtil.getSysOption(lhotelGroupCode, TaobaoConstant.CONFIG_CATEGORY,TaobaoConstant.CONFIG_ITEM.SESSIONKEY);
        if(StringUtil.isNotBlank(taobaoSessionKey)){
            return taobaoSessionKey;
        }else {
            throw new TaobaoException(QuintessonSysExceptionEnum.NO_TAOBAOSESSIONKEY,lhotelGroupCode);
        }
    }

    public static String getMatchInfo(int matchStatus){
        switch (matchStatus) {
            case 0:
                return TaobaoMatchStatusEnum.ZERO.getMatchInfo();
            case 1:
                return TaobaoMatchStatusEnum.ONE.getMatchInfo();
            case 2:
                return TaobaoMatchStatusEnum.TWO.getMatchInfo();
            case 3:
                return TaobaoMatchStatusEnum.THREE.getMatchInfo();
            case 4:
                return TaobaoMatchStatusEnum.FOUR.getMatchInfo();
            case 5:
                return TaobaoMatchStatusEnum.FIVE.getMatchInfo();
            case 6:
                return TaobaoMatchStatusEnum.SIX.getMatchInfo();
            case 7:
                return TaobaoMatchStatusEnum.SEVEN.getMatchInfo();
            default:
                return TaobaoMatchStatusEnum.NONE.getMatchInfo();
        }
    }
}
